package com.mymoney.helpers;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.text.InputType;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.mymoney.MyMoneyActivity;
import com.mymoney.ws.asynctasks.AddItemAsyncTask;
import com.mymoney.ws.data.DeviceInformation;
import com.mymoney.ws.data.ItemBean;

public class ItemsHelper {

    @SuppressWarnings("unused")
	private static final String TAG = "ItemsHelper";
    
	private MyMoneyActivity context;
    
    public ItemsHelper(MyMoneyActivity context)
    {
    	this.context = context;
    }
    
    public String[] getPlannedItesm(ItemBean[] listItems)
    {
    	String[] list = new String[listItems.length];
    	
    	for(int idx = 0; idx < listItems.length; idx++)
    	{
    		list[idx] = listItems[idx].getName();
    	}
    	
		return list;
    	
    }
    
    public void showItemDialog(final MyMoneyActivity context, final ItemBean item)
    {
    	if(item != null)
    	{
	    	AlertDialog.Builder alert = new AlertDialog.Builder(context);
	
	    	alert.setTitle(item.getName());
	    	alert.setMessage("Моля въведете цена");
	
	    	// Set an EditText view to get user input 
	    	final EditText input = new EditText(context);
	    	input.setRawInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
	    	
		    //Set Font
		    Typeface tf = Typeface.createFromAsset(context.getAssets(),
	                "fonts/SEGOE.TTF");
		    input.setTypeface(tf);
		    
	    	alert.setView(input);

	    	alert.setPositiveButton("Запиши", new DialogInterface.OnClickListener() {
		    	public void onClick(DialogInterface dialog, int whichButton) {
		    	
		    	//Input content validation
		    		if(input.getText().length() > 0)
		    		{
				    	String value = input.getText().toString();
						item.setPrice(Double.parseDouble(value));
						context.onReadyToBuy(item);
						hideoftKeyboard(input);
		    		}
		    		else
		    		{
		    			hideoftKeyboard(input);
		    		}
		    	}
	    	});
	
	    	alert.setNegativeButton("Отказ", new DialogInterface.OnClickListener() {
	    	  public void onClick(DialogInterface dialog, int whichButton) {
	    		  hideoftKeyboard(input);
	    	  	}
	    	});
	    	
	    	showSoftKeyboard();
	    	alert.show();
    	}
    }
    
    public void regisetrItemDialog()
    {
    	AlertDialog.Builder alert = new AlertDialog.Builder(context);

    	alert.setTitle("Планирай покупка");
    	alert.setMessage("Моля въведете продукт");

    	// Set an EditText view to get user input 
    	final EditText input = new EditText(context);
    	input.setRawInputType(InputType.TYPE_CLASS_TEXT);
    	
	    //Set Font
	    Typeface tf = Typeface.createFromAsset(context.getAssets(),
                "fonts/SEGOE.TTF");
	    input.setTypeface(tf);
	    
    	alert.setView(input);

    	alert.setPositiveButton("Запиши", new DialogInterface.OnClickListener() {
	    	public void onClick(DialogInterface dialog, int whichButton) {
	    	
	    		hideoftKeyboard(input);
	    		//Input content validation
	    		if(input.getText().length() > 0)
	    		{
	    			DeviceInfoHelper deviceInfoHelper = new DeviceInfoHelper(context);
	    			DeviceInformation deviceInfo = deviceInfoHelper.getDeviceInformation();
	    			if(deviceInfo != null)
	    			{
		    			ItemBean item = new ItemBean();
		    			item.setName(input.getText().toString());
		    			item.setBought(0);
		    			item.setPrimaryAccount(deviceInfo.getPrimaryAccount());
		    			
		    			AddItemAsyncTask addItem = new AddItemAsyncTask(context, item) {
							
							@Override
							public void onFinish() {
								context.showToast("Продуктът е планиран");
								try {
									Thread.sleep(1000);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								context.reload();
							}
						};
						
						addItem.execute();
	    			}
	    		}
	    		else
	    		{
	    			hideoftKeyboard(input);
	    		}
	    	}
    	});

    	alert.setNegativeButton("Отказ", new DialogInterface.OnClickListener() {
    	  public void onClick(DialogInterface dialog, int whichButton) {
    		  hideoftKeyboard(input);
    	  	}
    	});
    	
    	showSoftKeyboard();
    	alert.show();
    }
    
    private void showSoftKeyboard()
    {
    	((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    
    private void hideoftKeyboard(View ev)
    {
    	((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(ev.getWindowToken(), 0);
    }
}
